package com.crazypos.controller.admin;

import com.crazypos.common.ApiRestResponse;
import com.crazypos.pojo.MenuEntity;
import com.crazypos.pojo.ModuleEntity;
import com.crazypos.pojo.bo.*;
import com.crazypos.service.IMenuService;
import com.crazypos.service.IModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/menu")
public class AdminMenuController {

    @Autowired
    private IModuleService moduleService;

    @Autowired
    private IMenuService menuService;

    @GetMapping("/module_list")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse getAllModules() {
        List<ModuleEntity> result = moduleService.getAllModule();
        return ApiRestResponse.success(result);
    }

    @PostMapping("/module_create")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse createModule(@RequestBody @Validated AdminCreateModuleBO bo) {
        moduleService.createModule(bo);
        return ApiRestResponse.success();
    }

    @PostMapping("/module_update")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse updateModule(@RequestBody @Validated AdminUpdateModuleBO bo) {
        moduleService.updateModule(bo);
        return ApiRestResponse.success();
    }

    @PostMapping("/module_delete")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse deleteModule(@RequestBody @Validated AdminDeleteModuleBO bo) {
        moduleService.deleteModule(bo);
        return ApiRestResponse.success();
    }

    @GetMapping("/menu_list")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse getAllMenu(
             @DefaultValue("") Integer moduleId,
             @DefaultValue("") Integer status,
             @DefaultValue("") String keyword
    ) {
        List<MenuEntity> result = menuService.getAllMenu(moduleId, status,keyword);
        return ApiRestResponse.success(result);
    }

    @PostMapping("/menu_create")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse createMenu(@RequestBody @Validated AdminCreateMenuBO bo) {
        menuService.createMenu(bo);
        return ApiRestResponse.success();
    }

    @PostMapping("/menu_update")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse updateMenu(@RequestBody @Validated AdminUpdateMenuBO bo) {
        menuService.updateMenu(bo);
        return ApiRestResponse.success();
    }

    @PostMapping("/menu_delete")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse deleteMenu(@RequestBody @Validated AdminDeleteMenuBO bo) {
        menuService.deleteMenu(bo);
        return ApiRestResponse.success();
    }

}
